<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $components = array('Auth');
	function index() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->User->recursive = 0;
			$this->set('users', $this->paginate());
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function view($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid user', true));
				$this->redirect(array('action' => 'index'));
			}
			$this->set('user', $this->User->read(null, $id));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function add() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
		} else {
			$this->layout = 'cartlayout';
		}
		
		if($user['user_role']=='client'){
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->setDefault($this->data);
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				if($user['user_role']=='admin'){
					$this->redirect(array('controller'=>'users','action' => 'index'));
				} else {
					$this->redirect(array('controller'=>'pages','action' => 'index'));
				}
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
	}
/*
	function edit($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin' || $id==$user['id']){
			if($user['user_role']=='admin'){
				$this->layout = 'admin';
			}
			if (!$id && empty($this->data)) {
				$this->Session->setFlash(__('Invalid user', true));
				$this->redirect(array('action' => 'index'));
			}
			if (!empty($this->data)) {
				$this->setDefault($this->data);
				if ($this->User->save($this->data)) {
					$this->Session->setFlash(__('The user has been saved', true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->User->read(null, $id);
			}
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
*/
	function delete($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid id for user', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->User->delete($id)) {
				$this->Session->setFlash(__('User deleted', true));
				$this->redirect(array('action'=>'index'));
			}
			$this->Session->setFlash(__('User was not deleted', true));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function login(){
		if ($this->Session->read('Auth.User')) {
			$this->Session->setFlash('You are logged in!');
			$this->redirect('/users', null, false);
			
		}
	}			
			
	
	function logout(){
    	$this->redirect($this->Auth->logout());
	}
	
	function setDefault(&$data) {
			if($data['User']['user_role']=='0') {
				$data['User']['user_role']='client';
			}
	}
	function profile($id = null){
		$user = $this->Session->read('Auth.User');
		$this->loadModel('Product');
		$this->loadModel('Comment');
		$this->loadModel('Score');
		$this->loadModel('Sale');
		$comment = $this->Comment->find('all',array('conditions'=>array('user_id'=>$user['id']),'order'=>'created desc','limit'=>5));
		$score = $this->Score->find('all',array('conditions'=>array('user_id'=>$user['id']),'order'=>'created desc','limit'=>5));
		$sale = $this->Sale->find('all',array('conditions'=>array('user_id'=>$user['id']),'order'=>'created desc','limit'=>5));
		//$myarray = $this->set(array('Comment'=>'comment','Sale'=>'sale','Score'=>'score'));
		$this->set(compact('comment','score','sale'));
		$this->set($this->paginate());
		/*pr($user);
		pr($comment);
		pr($score);
		pr($sale);*/
		
	}
	function changepassword($id = null){
		$user = $this->Session->read('Auth.User');	
		$this->User->id = $user['id'];
		if(!$user && empty($this->data)) {
			$this->Session->setFlash(__('Geçersiz kullanıcı!', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			$pass = Security::hash($this->data['User']['old_pass'], null, true);
			$new_pass = Security::hash($this->data['User']['new_pass'], null, true);
			
			$passDB = $this->User->field('password');
			$this->User->set( $this->data );

			if($passDB==$pass) {
				if ($this->User->saveField('password', $new_pass, true) && $this->User->validates()) {
					$this->Session->setFlash(__('Şifre değiştirildi...', true));
					$this->redirect(array('controller'=>'pages', 'action' => 'index'));
				} else {
					$this->Session->setFlash(__('Şifre değiştirilemedi, Lütfen yeniden deneyin.', true));
				}
			} else {
				$this->Session->setFlash(__('Şifrenizi doğru girdiğinizden emin olun.', true));
			}
		}		
		
	}
	function profileupdate($id = null) {
			$user = $this->Session->read('Auth.User');
			if($id==$user['id']){
				if (!$id && empty($this->data)) {
					$this->Session->setFlash(__('Invalid user', true));
					$this->redirect(array('action' => 'index'));
				}
				if (!empty($this->data)) {
					$this->setDefault($this->data);
					if ($this->User->save($this->data)) {
						$this->Session->setFlash(__('Bilgileriniz Güncellenmiştir', true));
						//unset($this->data['User']['password']);
						//$this->data['User'] = array_values($this->data['User']);
						$myarray = array(
								'id'=>$this->data['User']['id'],
								'username'=>$this->data['User']['username'],
								'user_role'=>$this->data['User']['user_role'],
								'fname'=>$this->data['User']['fname'],
								'lname'=>$this->data['User']['lname'],
								
								'bdate'=>$this->data['User']['bdate'],
								'email'=>$this->data['User']['email'],
								'gsm'=>$this->data['User']['gsm'],
								'address'=>$this->data['User']['address']
								);
						$this->Session->write('Auth.User',$myarray);
						pr($this->data);
						//pr($this->Session->read('Auth.User'));
						//$this->redirect(array('controller'=>'users','action' => 'profile'));
					} else {
						$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
					}
				}
				if (empty($this->data)) {
					$this->data = $this->User->read(null, $id);
					$this->data['User']['password']='';										
				}
			} else {
				$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
				$this->redirect(array('controller'=>'pages','action' => 'index'));
			}
	}  
	function beforeFilter() {
	    parent::beforeFilter();
	    $this->Auth->allow(array('*'));
    }	
	
}
